package com.dyjch.mly.controller;

import com.dyjch.mly.entity.GoodsBound;
import com.dyjch.mly.service.imp.GoodsInBoundServiceImp;
import com.dyjch.mly.service.imp.GoodsManagerServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Date;
import java.util.List;

@Controller
public class GoodsInBoundController {
    @Autowired
    GoodsInBoundServiceImp goodsInBoundService;

    @Autowired
    GoodsManagerServiceImp goodsManagerService;


    @RequestMapping("/inbound.html")
    public String list(Model model){
        List<GoodsBound> goods=goodsInBoundService.getAllMessage();
        model.addAttribute("mess",goods);
        return "inbound";
    }

    @PostMapping("/GoodsInBoundOperation")
    public String GoodsInBound(String name,String type,int number){
        Date date=new Date(new java.util.Date().getTime());
        GoodsBound goodsBound = new GoodsBound(0, name, type, number, date);
        goodsInBoundService.addGoodsInBoundMessage(goodsBound);

        if (goodsManagerService.queryGoodsByName(name)==null){
            goodsManagerService.addGoods(goodsBound);
        }else {
            goodsManagerService.updateWithNumber(name,number);
//            goodsManagerService.updateWithNumber(name,-number);
        }

        return "redirect:inbound.html";
    }
}
